<?php
class Forget extends BZ_Controller
{
    public function _init()
    {
        $this->load->helper(array('email'));
        $this->load->library(array('encrypt', 'sms/sms'));
        $this->load->model('user_model', 'user');
    }
    
    /**
     * 填写账户名称
     */
    public function index()
    {
        if ($this->frontUser) { //如果已经登录，就跳转到首页。
            $this->redirect($this->config->wap_base_url);
        }
        $captcha = $this->getCaptcha(18, 130, 36);
        $data['captcha'] = $captcha;
        $this->load->view('forget/index', $data);
    }
    
    /**
     * 验证码
     */
    public function ajaxJsonCaptcha()
    {
        $captcha = $this->getCaptcha(18, 130, 36, 4);
        echo json_encode($captcha);exit;
    }
    
    /**
     * 验证账户
     */
    public function alidateUser()
    {
        $username = $this->input->post('username');
        if ($this->validateParam($username)) {
            $this->ajaxJson(false, '用户名必填');
        }
        if (strtolower($this->input->post('captcha')) != strtolower(get_cookie('captcha'))) {
            $this->ajaxJson(false, '验证码错误');
        }
        $result = $this->user->validateName($username);
        if ($result->num_rows() <= 0) {
            $this->ajaxJson(false, '用户名不存在');
        }
        $encodename = $this->encrypt->encode($username);
        
        $this->ajaxJson(true, site_url('forget/confirm').'?keycode='.urlencode($encodename));
    }
    
    /**
     * 使用手机或邮箱找回
     */
    public function confirm()
    {
        if ($this->frontUser) { //如果已经登录，就跳转到首页。
            $this->redirect($this->config->www_base_url);
        }
        $username = urldecode($this->encrypt->decode($this->input->get('keycode')));
        $result = $this->user->validateName($username);
        if ($result->num_rows() <= 0) {
            $this->alertJumpPre('帐号有误，请联系客服解决问题');
        }
        $user = $result->row();
        if (valid_mobile($username)) {
            $mobilePhone = $username;
        } else {
            $mobilePhone = $user->mobile_phone;
        }
        $data['user_name'] = $this->encrypt->encode($user->user_name);
        $data['encode_phone'] = $this->encrypt->encode($mobilePhone);
        $data['mobile_phone'] = substr_replace($mobilePhone, '****', 3, 4);
        $this->load->view('forget/confirm', $data);
    }
    
    //发送短信
    public function checkPhone()
    {
        $phone = $this->encrypt->decode($this->input->post('phone'));
        if (!valid_mobile($phone)) {
            $this->ajaxJson(false, '手机号码有误');
        }

        return $this->sendCommonSms($phone, 'SMS_136393815');
    }
    
    /**
     * 使用手机或邮箱找回验证
     */
    public function confirmValidate()
    {
        $mobilePhone = $this->encrypt->decode($this->input->post('mobile_phone'));
        $encodeName = $this->input->post('username');
        $code = $this->input->post('verify');
        $endTime = date('Y-m-d H:i:s');
        if (!valid_mobile($mobilePhone)) {
            $this->ajaxJson(false, '手机号码有误');
        }

        $result = $this->user_code->findOne(['mobile' => $mobilePhone, 'code' => $code], 'id, end_time');//, 'end_time' => $endTime
        if ($result->num_rows() <= 0) {
            $this->ajaxJson(false, '手机验证码错误');
        }
        if ($result->row(0)->end_time < $endTime) {
            $this->ajaxJson(false, '手机验证码超过有效期，请重新获取~');
        }
        
        $this->ajaxJson(true, base_url('forget/modify').'?keycode='.urlencode($encodeName));
    }
    
    /**
     * 设置密码页面
     */
    public function modify()
    {
        if ($this->frontUser) { //如果已经登录，就跳转到首页。
            $this->redirect($this->config->www_base_url);
        }
        $username = urldecode($this->encrypt->decode($this->input->get('keycode')));
        $result = $this->user->validateName($username);
        if ($result->num_rows() <= 0) {
            $this->alertJumpPre('帐号有误，请联系客服解决问题');
        }
        $user = $result->row();
        $data['user'] = $user;
        $data['username'] = $this->encrypt->encode($user->user_name);
        $this->load->view('forget/modify', $data);
    }
    
    /**
     * 设置密码页面
     */
    public function modifyValidate()
    {
        $postData = $this->input->post();
        if ($this->input->post('password') != $this->input->post('confirm_password')) {
            $this->ajaxJson(false, '密码输入不一致');
        }
        $username = urldecode($this->encrypt->decode($this->input->post('username')));
        $result = $this->user->validateName($username);
        if ($result->num_rows() <= 0) {
            $this->ajaxJson(false, '不可修改密码，请联系管理员');
        }
        $postData['username'] = $username;
        $modify = $this->user->modifyPassword($postData);
        if (!$modify) {
            $this->ajaxJson(false, '服务器忙，请稍候再试');
        }
        $this->ajaxJson(true, base_url('forget/success'));
    }
    
    /**
     * 设置密码成功页面
     */
    public function success()
    {
        if ($this->frontUser) { //如果已经登录，就跳转到首页。
            $this->redirect($this->config->www_base_url);
        }
        $this->load->view('forget/success');
    }
}
